﻿
using System.Data;
using System.Text;
using System.Threading;
using static Models.Common.EnumDeviceState;
using GatewayServerService.Data;
using Microsoft.EntityFrameworkCore;
using UtilsCache;
using UtilsORM;
using UtilsORM.MySqlData;

namespace GatewayServerService.CloudCenter
{
    public class ServiceCloudDeviceLiveState
    {

        /// <summary>
        /// 更新设备状态
        /// </summary>
        /// <param name="dguid"></param>
        /// <param name="state"></param>
        /// <param name="remark"></param>
        public void setDeviceLiveState(string dguid, int state, string remark = "")
        {

            try
            {
                //string respStatus = UtilMemoryCacheHelper.get<string>("setDeviceLiveState" + dguid);
                //if (!string.IsNullOrEmpty(respStatus) && respStatus== state.ToString())
                //{//判断重复处理
                //    return;
                //}
                //UtilMemoryCacheHelper.set<string>("setDeviceLiveState" + dguid, state.ToString(),10);

                EFYihongIotContext ef = new EFYihongIotContext();
             
                int ls_response_state = 0;
                if (state == 2)
                {
                    ls_response_state = 1;
                }
                int count = ef.iot_device_live_state.Where(c => c.ls_d_guid == dguid).ExecuteUpdate(c => c
                .SetProperty(p => p.ls_live_state, p => state)
                .SetProperty(p => p.ls_response_state, ls_response_state)
                .SetProperty(p => p.ls_live_state_set_time, DateTime.Now)
                .SetProperty(p => p.ls_live_state_remark, remark));

                // string sql = "update iot_device_live_state set ls_response_state=0,ls_live_state='" + state + "',ls_live_state_set_time='" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "',ls_live_state_remark=@ls_live_state_remark where ls_d_guid='" + dguid + "'";
                //  int count = new MySqlYihongIot().Db.Ado.ExecuteCommand(sql, new { ls_live_state_remark = remark });
                if (count < 1)
                {
                    iot_device_live_state live_State = new iot_device_live_state();
                    live_State.ls_guid = Guid.NewGuid().ToString();
                    live_State.ls_d_guid = dguid;
                    live_State.ls_live_state = state;
                    live_State.ls_live_state_set_time = DateTime.Now;
                    live_State.ls_live_state_remark = remark;
                    live_State.ls_add_time = DateTime.Now;
                    live_State.ls_response_state = 0;
                    ef.iot_device_live_state.Add(live_State);
                    ef.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        public void setUnResponse(string dguid, string remark = "")
        {
            try
            {

                if (string.IsNullOrWhiteSpace(dguid))
                {
                    return;
                }
                EFYihongIotContext ef = new EFYihongIotContext();

                ef.iot_device_live_state.Where(c => c.ls_d_guid == dguid && c.ls_response_state == 0).ExecuteUpdate(c => c
               .SetProperty(p => p.ls_response_state, 1)
               .SetProperty(p => p.ls_unresponse_time, DateTime.Now)
               .SetProperty(p => p.ls_live_state_remark, remark));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

    }

}
